var mContext = '/zdmConsole';

// 分页用到的方法
function turnOverPage(no) {
	document.qPagerForm.pageNo.value = no;
	var listUrl = $('#qPagerForm').attr('action');
	mySubmit('qPagerForm', listUrl);
}

function loading() {
	$("#overlayDiv").css("min-height", $(document).height());
	$("#overlayDiv").show();
}

function closeLoading() {
	$("#overlayDiv").hide();
}

function stopBubble(e) {
	if (e && e.stopPropagation) {
		e.stopPropagation();
	} else {
		window.event.cancelBubble = true;
	}
}

$.fn.serializeObject = function() {
	var o = {};
	var a = this.serializeArray();
	$.each(a, function() {
		if (o[this.name]) {
			if (!o[this.name].push) {
				o[this.name] = [ o[this.name] ];
			}
			o[this.name].push(this.value || '');
		} else {
			o[this.name] = this.value || '';
		}
	});
	return o;
};

var formData;


/**
 * 把服务器返回字符串解析成json格式
 * @param dataResponse
 * @param callback
 * @returns
 */
function parseResult(dataResponse,callbackSuccess,callbackError){
	var obj;
	if(Object.prototype.toString.call(dataResponse)==='[object String]'){
		obj = JSON.parse(dataResponse);
	}else{
		obj=dataResponse;
	}
	
	if (obj.code == '0000') {
		callbackSuccess(obj);
	} else {
		alert(obj.msg);
		callbackError(obj);
	}
}

// 如果formid为"",就是get否则就是post
// 如果是post，successUrl不为""成功后会通过mySubmit调用successUrl
// 成功后都会调用callback
function myAjaxSumbmit(formId, formUrl, successUrl, callback, isJson) {
	formUrl = mContext + formUrl;
	if (formId == '') {
		// get
		$.ajax({
			async : false,
			type : "GET",
			url : formUrl,
			success : function(data) {
				var obj;
				if(Object.prototype.toString.call(data)==='[object String]'){
					obj = JSON.parse(data);
				}else{
					obj=data;
				}
				
				if (obj.code == '0000') {
					callback(obj.data);
				} else {
					alert(obj.msg);
				}
			},
			error : function(errMsg) {
				alert(errMsg.status + "失败---" + errMsg.text);
			}
		});
	} else {
		if (formId != "noData") {

			if (isJson) {
				var fDate = $("#" + formId).serializeObject();
			} else {
				var fData = $("#" + formId).serialize();
			}
			// fData = _fixFormData(fData);
		} else {
			var fData = "";
		}
		// post
		$.ajax({
			data : fData,
			type : "POST",
			url : formUrl,
			success : function(data) {
				var obj;
				if(Object.prototype.toString.call(data)==='[object String]'){
					obj = JSON.parse(data);
				}else{
					obj=data;
				}
				if (obj.code == '0000') {
					alert(obj.msg);
					if (callback != null) {
						if (typeof callback === 'function') {
							callback(obj.data);
						}
					}
					if (successUrl) {
						mySubmit('', successUrl);
					}
				} else {
					alert(obj.msg);
				}
			},
			error : function(errMsg) {
				alert(errMsg.status + "失败---" + errMsg.text);
			}
		});
	}
}

function myMenuSubmit(obj, formId, formUrl, urlTitle, urlDesc, menuTitle,
		categoryTitle) {
	$("li.treeview.active").removeClass("active");
	$(obj).parent().addClass("active");
	mySubmit(formId, formUrl, urlTitle, urlDesc, menuTitle, categoryTitle);
}

function mySubmit(formId, formUrl, urlTitle, urlDesc, menuTitle, categoryTitle) {
	loading();

	formUrl = mContext + formUrl;

	if (!(urlTitle === undefined) && urlTitle != '') {
		$("#urlTitle").hide();
		$("#menuTitle").hide();
		$(".categorys").remove();

		if (!(urlDesc === undefined) && urlDesc != '' && urlDesc != "null") {
			urlTitle = urlTitle + "<small>" + urlDesc + "</small>";
		}
		$("#urlTitle").html(urlTitle);
		$("#urlTitle").show();
	}

	if (!(categoryTitle === undefined) && categoryTitle != '') {
		var categorys = categoryTitle.split(",");
		var cs = "";
		for (var i = 0; i < categorys.length; i++) {
			cs = cs + '<li class="categorys">' + categorys[i] + '</li>';
		}
		$("#homeTitle").after(cs);

		// $("#categoryTitle").html(categoryTitle);
		// $("#categoryTitle").show();
	}
	if (!(menuTitle === undefined) && menuTitle != '') {
		$("#menuTitle").html(menuTitle);
		$("#menuTitle").show();
	}

	if (formId == '') {
		$("#contentS").load(formUrl, function(responseTxt, statusTxt, xhr) {
			if (statusTxt == "success") {
				// alert("页面加载成功！");
			}
			if (statusTxt == "error") {
				alert("Error: " + xhr.status + ": " + xhr.statusText);
			}
			closeLoading();
			;
		});
	} else {
		if (formId != 'history') {
			formData = $("#" + formId).serialize();
			formData = fixFormData(formData);
		}
		$("#contentS").load(formUrl, formData,
				function(responseTxt, statusTxt, xhr) {
					if (statusTxt == "success") {
						// alert("页面加载成功！");
					} else if (statusTxt == "error") {
						alert("Error: " + xhr.status + ": " + xhr.statusText);
					}
					closeLoading();
				});
	}
	// closeLoading();
	// $("div.modal-backdrop").remove();
}

function closeModal(modalName) {
	$('#' + modalName).modal('hide');
}

function fixFormData(formData) {
	formData = formData.replace(/\+/g, " ");
	var tmp = formData.split("&");
	formData = "";
	for (var i = 0; i < tmp.length; i++) {
		if (tmp[i].charAt(tmp[i].length - 1) != "=") {
			formData = formData + tmp[i] + "&";
		}
	}
	if (formData.length > 1) {
		formData = formData.substring(0, formData.length - 1);
	}
	return formData;
}